//
//  HCStarDidView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/20.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCStarDidView.h"

#import "HCStarSelectedButton.h"

#import "HCStarDidModel.h"


@interface HCStarDidView () <CWStarRateViewDelegate,HCStarSelectedButtonDelegate> {
    HCStarSelectedButton *selectedButton;
    UILabel *promptLabel;
    NSMutableArray *arr_buttons;
    HCStarDidData *data;
    UIImageView *imageView;
    UILabel *nameLabel;
}

@end

@implementation HCStarDidView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubView:frame];
    }
    return self;
}

-(void)initializeSubView:(CGRect)frame {
    
    arr_buttons = [NSMutableArray array];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, YBKWinFrame.size.width, YBKAdaptiveH(220))];
    imageView.image = [UIImage imageNamed:@"happy_stardid_header"];
    [self addSubview:imageView];
    
    UIView *burView = [[UIView alloc] initWithFrame:imageView.frame];
    burView.backgroundColor = [UIColor blackColor];
    burView.alpha = 0.4f;
    [imageView addSubview:burView];
    
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, imageView.frame.size.height - YBKAdaptiveH(30), imageView.frame.size.width - 10, 25)];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = YBKSystemFont(YBKAdaptiveFont(18));
    nameLabel.text = @"科华南路店";
    [imageView addSubview:nameLabel];
    
    self.starView = [[CWStarRateView alloc] initWithFrame:CGRectMake((frame.size.width - YBKAdaptiveW(192))/2.0, imageView.frame.origin.y+imageView.frame.size.height+YBKAdaptiveH(25), YBKAdaptiveW(192), YBKAdaptiveH(24)) numberOfStars:5];
    self.starView.allowTouch = YES;
    self.starView.scorePercent = 0;
    self.starView.delegate = self;
    [self addSubview:self.starView];
    
    promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.starView.frame.origin.y+self.starView.frame.size.height+YBKAdaptiveH(20), frame.size.width, 15)];
    promptLabel.textAlignment = NSTextAlignmentCenter;
    promptLabel.textColor = YBKRGBCOLOR(240, 96, 96, 1);
    promptLabel.text = @"比较满意，仍可改善";
    promptLabel.font = YBKSystemFont(YBKAdaptiveFont(13));
    [self addSubview:promptLabel];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(YBKAdaptiveW(10), frame.size.height - YBKAdaptiveW(10)-YBKAdaptiveH(50)- YBKAdaptiveH(50), frame.size.width - 2.0*YBKAdaptiveW(10), YBKAdaptiveH(50))];
    btn.backgroundColor = YBKRGBCOLOR(240, 96, 96, 1);
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = YBKSystemFont(YBKAdaptiveFont(13));
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    btn.layer.cornerRadius = 5.0f;
    btn.userInteractionEnabled = YES;
    [btn addTarget:self action:@selector(sureBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    
    
}

#pragma mark 评分回调
- (void)starRateView:(CWStarRateView *)starRateView scroePercentDidChange:(CGFloat)newScorePercent {
    starRateView.scorePercent = newScorePercent;
    if (selectedButton) {
        [selectedButton reloadDataWith:newScorePercent];
    }else{
        selectedButton = [[HCStarSelectedButton alloc] initWithFrame:CGRectMake(YBKAdaptiveW(50), promptLabel.frame.origin.y+promptLabel.frame.size.height+YBKAdaptiveW(50), self.frame.size.width - 2.0*YBKAdaptiveW(50), YBKAdaptiveH(117)) withScore:newScorePercent];
        selectedButton.delegate = self;
        [self addSubview:selectedButton];
    }
    
    if (newScorePercent<=0.8) {
        promptLabel.text = @"比较满意，仍可改善";
    }else{
        promptLabel.text = @"非常满意，无可改善";
    }
    
}

-(void)starSelectedButtonClicked:(NSMutableArray *)arr {
    NSLog(@"选中的数据:%@",arr);
    
    [arr_buttons removeAllObjects];
    
    for (int i = 0; i<arr.count; i++) {
        UIButton *btn = (UIButton *)arr[i];
        if (self.starView.scorePercent == 1) {
            [arr_buttons addObject:[NSString stringWithFormat:@"%ld",btn.tag+6]];
        }else {
            [arr_buttons addObject:[NSString stringWithFormat:@"%ld",(long)btn.tag]];
        }
        
    }
}

-(void)sureBtn:(UIButton *)btn {
    if (arr_buttons.count<=0) {
        [XHToast showBottomWithText:@"请选择至少一项评价"];
    }else{
        if ([self.delegate respondsToSelector:@selector(sureClicked:andStar:)]&&self.delegate) {
            NSString *buttons_str = [arr_buttons componentsJoinedByString:@","];
            NSString *star = [NSString stringWithFormat:@"%f",self.starView.scorePercent*5];
            [self.delegate sureClicked:buttons_str andStar:star];
        }
    }
}

-(void)setModel:(id)model {
    data = (HCStarDidData *)model;
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.logo]]];
    nameLabel.text = [NSString stringWithFormat:@"%@",data.name];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
